﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace simulate_control.Basic
{
    #region USING
    using simulate_control.Repositories;
    using simulate_control.Models;
    using simulate_control.Helpers;
    using System.Globalization;
    #endregion

    public partial class FrmSystemSetup : Form
    {
        #region VARIABLES
        public EventHandler Handler { get; set; }
        private Dictionary<int, string> dict = new Dictionary<int,string>();

        // Bien danh dau hien thoi dang lua chon item nao
        private int Selected = 0;

        // Bien danh dau item da di vao trang thai edit hay chua
        private bool EnterEdit = false;

        // Ki tu trong item duoc lua chon 
        private int SelectedCharacterPos = 0;
        // 3 mang dung de phan loai item thuoc ve nhom nao: time, character hay value
        private string[] arrTimeVal = new string[] { "lblTimeDiff" };
        private string[] arrChar = new string[] { "lblDatum", "lblUnits" };
        private string[] arrVal = new string[] { "lblTimeDisp" };

        // Bien chua dung danh sach ten cac label item
        private string[] lstHighlightLabelName = new string[] { "lblDatum", "lblUnits", "lblTimeDiff", "lblTimeDisp" };
        #endregion

        #region METHODS
        public FrmSystemSetup()
        {
            InitializeComponent();
        }

        public void ProcessKeyEvent(KEY keyEvent)
        {
            // Da di vao trang thai edit
            if (EnterEdit)
            {
                // Nhan enter de cap nhat gia tri, va thoat khoi trang thai edit
                if (keyEvent == KEY.ENTER)
                {
                    Utils.RetrieveToNextItemPos(pnlMain, dict, ref Selected);
                    // Thoat khoi trang thai edit
                    EnterEdit = false;
                }
                else
                {
                    // Cap nhat lai gia tri cho item duoc lua chon
                    var lbl = Utils.GetSelectedHighlightLabel(pnlMain, dict[Selected]);
                    Utils.ChangeCharacterPos(ref lbl, dict, Selected, ref SelectedCharacterPos, keyEvent, arrTimeVal, arrChar, arrVal);
                }
            }
            else // Chua di vao trang thai edit
            {
                // Nhan enter de di vao trang thai edit
                if (keyEvent == KEY.ENTER)
                {
                    SelectedCharacterPos = 0;
                    var lbl = Utils.GetSelectedHighlightLabel(pnlMain, dict[Selected]);
                    Utils.UpdateHighlightLabels(ref lbl, SelectedCharacterPos);
                    // Di vao trang thai edit
                    EnterEdit = true;
                }
                else
                {
                    // Lua chon item de edit
                    Utils.ChangeSelectItemPos(pnlMain, dict, ref Selected, keyEvent);
                }
            }
        }

        public void ReInitLabels(string[] lbls)
        {
            foreach (var str in lbls)
            {
                var lbl = Utils.GetSelectedHighlightLabel(pnlMain, str);

                switch (str)
                {
                    case "lblTimeDiff":
                        lbl.LabelText = "00:00";
                        lbl.LabelMaxLength = lbl.LabelText.Length;
                        break;

                    case "lblTimeDisp":
                        lbl.LabelText = "00";
                        lbl.LabelMaxLength = lbl.LabelText.Length;
                        break;
                }
            }   
        }

        private void FrmSystemSetup_Load(object sender, EventArgs e)
        {
            // Init Time diff
            Utils.InitHighlightLabels(ref pnlMain, lstHighlightLabelName);
            
            Utils.InitDict(ref dict, lstHighlightLabelName);
            dict.Add(4, "lblTest");
            dict.Add(5, "lblSimulator");
            dict.Add(6, "lblExchangeBattery");
            
            Utils.ChangeSelectItem(pnlMain, dict, 0);

            string[] reInitLbls = new string[] { "lblTimeDiff", "lblTimeDisp" };
            ReInitLabels(reInitLbls);
        }
        #endregion
    }
}
